package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SalaryStandard;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.TempUserSalaryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AchievementsMapper;
import com.ruoyi.system.domain.Achievements;
import com.ruoyi.system.service.IAchievementsService;
import com.ruoyi.common.core.text.Convert;

/**
 * 人员绩效Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-13
 */
@Service
public class AchievementsServiceImpl extends ServiceImpl<AchievementsMapper,Achievements> implements IAchievementsService
{
    @Autowired
    private AchievementsMapper achievementsMapper;

    /**
     * 查询人员绩效
     * 
     * @param id 人员绩效主键
     * @return 人员绩效
     */
    @Override
    public Achievements selectAchievementsById(Long id)
    {
        return achievementsMapper.selectAchievementsById(id);
    }

    /**
     * 查询人员绩效列表
     * 
     * @param achievements 人员绩效
     * @return 人员绩效
     */
    @Override
    public List<Achievements> selectAchievementsList(Achievements achievements)
    {
        return achievementsMapper.selectAchievementsList(achievements);
    }

    /**
     * 新增人员绩效
     * 
     * @param achievements 人员绩效
     * @return 结果
     */
    @Override
    public int insertAchievements(Achievements achievements)
    {
        String userName = ShiroUtils.getSysUser().getUserName();
        Date nowDate = DateUtils.getNowDate();
        achievements.setCreateTime(nowDate);
        achievements.setUpdateTime(nowDate);
        achievements.setUpdateBy(userName);
        achievements.setCreateBy(userName);
        return achievementsMapper.insertAchievements(achievements);
    }

    /**
     * 修改人员绩效
     * 
     * @param achievements 人员绩效
     * @return 结果
     */
    @Override
    public int updateAchievements(Achievements achievements)
    {
        String userName = ShiroUtils.getSysUser().getUserName();
        achievements.setUpdateBy(userName);
        achievements.setUpdateTime(DateUtils.getNowDate());
        return achievementsMapper.updateAchievements(achievements);
    }

    /**
     * 批量删除人员绩效
     * 
     * @param ids 需要删除的人员绩效主键
     * @return 结果
     */
    @Override
    public int deleteAchievementsByIds(String ids)
    {
        return achievementsMapper.deleteAchievementsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除人员绩效信息
     * 
     * @param id 人员绩效主键
     * @return 结果
     */
    @Override
    public int deleteAchievementsById(Long id)
    {
        return achievementsMapper.deleteAchievementsById(id);
    }

    @Override
    public List<TempUserSalaryDto> selectAchievementsData(Map<String, Object> map) {
        return achievementsMapper.selectAchievementsData(map);
    }
    /**
     * 导入人员绩效数据
     *
     * @param list 人员绩效数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作工资标准
     * @return 结果
     */
    @Override
    public String importAchievements(List<Achievements> list, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (Achievements achievements : list)
        {
            try
            {
                //根据导入数据查询表中是否存在这个月这个人的绩效信息
                int count = count(new LambdaQueryWrapper<Achievements>()
                        .eq(Achievements::getUserId, achievements.getUserId())
                        .eq(Achievements::getAchievementsDate,achievements.getAchievementsDate()));

                // 如果岗位存在，且表中没有岗位工资标准
                if (count==0)
                {
                    String userName = ShiroUtils.getSysUser().getUserName();
                    Date nowDate = DateUtils.getNowDate();
                    achievements.setCreateTime(nowDate);
                    achievements.setUpdateTime(nowDate);
                    achievements.setUpdateBy(userName);
                    achievements.setCreateBy(userName);
                    this.insertAchievements(achievements);
                    successNum++;
                    successMsg.append("<br/>" + failureNum + "、员工 " + achievements.getUserName() + " 的绩效信息导入成功");
                }
                else if (isUpdateSupport)
                {
                    String userName = ShiroUtils.getSysUser().getUserName();
                    Date nowDate = DateUtils.getNowDate();
                    achievements.setUpdateTime(nowDate);
                    achievements.setUpdateBy(userName);
                    this.updateAchievements(achievements);
                    successNum++;
                    successMsg.append("<br/>" + failureNum + "、员工 " + achievements.getUserName() + " 的绩效信息导入成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、员工 " + achievements.getUserName() + "在"+achievements.getAchievementsDate()+" 的绩效信息已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、员工 " + achievements.getUserName() + " 的绩效信息导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }


}
